Expand description

Lower-level API for interfacing with the NEAR Protocol via JSONRPC.

Layout

Each one the valid public JSON RPC methods are pre-defined in specialized modules within the methods module.

Inside every method module (e.g methods::query) there’s;

Calling a constructed request on a client returns with the response and error types for that method.

Examples

  1. Request server status from testnet RPC

    use near_jsonrpc_client::{methods, JsonRpcClient};
    
    let testnet_client = JsonRpcClient::connect("https://rpc.testnet.near.org");
    
    let status_request = methods::status::RpcStatusRequest; // no params
    
    // call a method on the server via the connected client
    let server_status = testnet_client.call(status_request).await?;
    
    println!("{:?}", server_status);
  2. Query transaction status from mainnet RPC

    use near_jsonrpc_client::{methods, JsonRpcClient};
    use near_jsonrpc_primitives::types::transactions::TransactionInfo;
    
    let mainnet_client = JsonRpcClient::connect("https://archival-rpc.mainnet.near.org");
    
    let tx_status_request = methods::tx::RpcTransactionStatusRequest {
        transaction_info: TransactionInfo::TransactionId {
            hash: "9FtHUFBQsZ2MG77K3x3MJ9wjX3UT8zE1TczCrhZEcG8U".parse()?,
            account_id: "miraclx.near".parse()?,
        },
    };
    
    let tx_status = mainnet_client.call(tx_status_request).await?;
    
    println!("{:?}", tx_status);
  3. For all intents and purposes, the predefined structures in methods should suffice, if you find that they don’t or you crave extra flexibility, well, you can opt in to use the generic constructor methods::any() with the any feature flag.

    In this example, we retrieve only the parts from the genesis config response that we care about.

    # in Cargo.toml
    near-jsonrpc-client = { ..., features = ["any"] }
    
    use serde::Deserialize;
    use serde_json::json;
    
    use near_jsonrpc_client::{methods, JsonRpcClient};
    use near_primitives::serialize::dec_format;
    use near_primitives::types::*;
    
    #[derive(Debug, Deserialize)]
    struct PartialGenesisConfig {
        protocol_version: ProtocolVersion,
        chain_id: String,
        genesis_height: BlockHeight,
        epoch_length: BlockHeightDelta,
        #[serde(with = "dec_format")]
        min_gas_price: Balance,
        #[serde(with = "dec_format")]
        max_gas_price: Balance,
        #[serde(with = "dec_format")]
        total_supply: Balance,
        validators: Vec<AccountInfo>,
    }
    
    impl methods::RpcHandlerResponse for PartialGenesisConfig {}
    
    let mainnet_client = JsonRpcClient::connect("https://rpc.mainnet.near.org");
    
    let genesis_config_request = methods::any::<Result<PartialGenesisConfig, ()>>(
        "EXPERIMENTAL_genesis_config",
        json!(null),
    );
    
    let partial_genesis = mainnet_client.call(genesis_config_request).await?;
    
    println!("{:#?}", partial_genesis);

Modules

Structs

Constants

Traits

Type Definitions